package com.tommy.jiami.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tommy.jiami.R;
import com.tommy.jiami.adapter.FileAdapter;
import com.tommy.jiami.ui.dialog.CryptDialog;
import com.tommy.jiami.utils.Constants;
import com.tommy.jiami.utils.Crypt;
import com.tommy.jiami.utils.Utils;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FileActivity extends AppCompatActivity
{
	File file;
	View headerView;
	FileAdapter fileAdapter;
	BroadcastReceiver receiver=new BroadcastReceiver(){

		@Override
		public void onReceive(Context Context, Intent intent)
		{
			updateFileList(false);
		}

	};
	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar) ;
		setSupportActionBar(toolbar);
		file=Environment.getExternalStorageDirectory();
		initHeaderView();
		getListView().setDividerHeight(0);
		updateFileList(true);
	}

	private void initHeaderView()
	{
		headerView=getLayoutInflater().inflate(R.layout.item_file,null);
		ImageView image=(ImageView)headerView.findViewById(R.id.image);
		TextView name=(TextView)headerView.findViewById(R.id.name);
		TextView length=(TextView)headerView.findViewById(R.id.length);
		image.setImageResource(R.drawable.dic);
		name.setText("...");
		length.setText(R.string.back_to_up);
	}

	@Override
	protected void onStart()
	{
		IntentFilter filter=new IntentFilter();
		filter.addAction(Constants.ACTION_CRYPT_OK);
		registerReceiver(receiver,filter);
		super.onStart();
	}

	@Override
	protected void onDestroy()
	{
		unregisterReceiver(receiver);
		super.onDestroy();
	}

	

	
	//更新文件列表
	private void updateFileList(boolean isFirst){
		try{
			
		File[] fl=file.listFiles(new FileFilter(){
			public boolean accept(File file){
				return !file.isHidden();
			}
		}); 
		
		//进行文件排序
		List<File> list1 = new ArrayList<>();
		List<File> list2 = new ArrayList<>();
		List<File> fileList = new ArrayList<>();
		for (File f : fl) {
			if (f.isDirectory()) {
				list1.add(f);
			}
			if (f.isFile()) {
				list2.add(f);
			}
		}
		Collections.sort(list1);
		Collections.sort(list2);
		fileList.addAll(list1);
		fileList.addAll(list2);
		
		String parent=file.getParent();
		setTitle(parent==null?file.toString():file.getName());
		getSupportActionBar().setSubtitle(parent);
		
		//判断是否为根目录(添加与删除header)
		if(!file.getAbsolutePath().equals("/")){
			if(getListView().getHeaderViewsCount()==0)getListView().addHeaderView(headerView);
		}else{
			getListView().removeHeaderView(headerView);
		}
		
		
		if(isFirst){		
			fileAdapter=new FileAdapter(this,fileList);
			getListView().setAdapter(fileAdapter);
		}else {
			fileAdapter.notifyDataSetChanged(fileList);
		}
		
		//排除需要root权限才能进入的目录
		}catch(NullPointerException e){
			Utils.showToast("因权限问题无法访问");
			file=file.getParentFile();
		}
	}

	public ListView getListView() {
		if(listView==null) {
			listView = (ListView) findViewById(R.id.list_view);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					int count=getListView().getHeaderViewsCount();
					position-=count;
					if(position<0){
						file=file.getParentFile();
						updateFileList(false);
					}else{
						File f=fileAdapter.getFileList().get(position);
						if(f.isDirectory()){
							file=f;
							updateFileList(false);
						}
						else{
							int cryptType=f.toString().endsWith(".jiami")?Crypt.TYPE_DECRYPT:Crypt.TYPE_ENCRYPT;
							CryptDialog dialog=new CryptDialog(f.toString(),cryptType);
							dialog.show(getFragmentManager(),"dialog");
						}
					}
				}
			});
		}
		return listView;
	}
}
